import {
  PersonModel
} from '../../models/personal.js'
let personModel = new PersonModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schoolName: '',
    fullTimes: [{name: '是',value: '1'},{name: '否',value: '2'}],
    fullTimeIdx: 0,
    eduType: '',
    major: '',
    entryDate: '',
    endDate: '',
    FItEmpId: '',
    FId: '',
  },

  fullTimeSelector(e) {
    this.setData({
      fullTimeIdx: e.detail.value
    })
  },

  schoolInput(e) {
    this.setData({
      schoolName: e.detail.value
    })
  },

  eduTypeInput(e) {
    this.setData({
      eduType: e.detail.value
    })
  },

  majorInput(e) {
    this.setData({
      major: e.detail.value
    })
  },

  entryDateSelector(e) {
    this.setData({
      entryDate: e.detail.value
    })
  },

  endDateSelector(e) {
    this.setData({
      endDate: e.detail.value
    })
  },

  checkData() {
    if (!this.data.schoolName) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写学校名称'
      })
      return
    }
    if (!this.data.eduType) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写学历'
      })
      return
    }
    if (!this.data.major) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写专业'
      })
      return
    }
    wx.showLoading()
    var param
    if (this.data.FId) {
      param = {
        FName: this.data.schoolName,
        FMajor: this.data.major,
        FEducationType: this.data.eduType,
        FIsFullTime: this.data.fullTimes[this.data.fullTimeIdx].value,
        FStartDate: this.data.entryDate,
        FEndDate: this.data.endDate,
        FId: this.data.FId
      }
    } else {
      param = {
        FName: this.data.schoolName,
        FMajor: this.data.major,
        FEducationType: this.data.eduType,
        FIsFullTime: this.data.fullTimes[this.data.fullTimeIdx].value,
        FStartDate: this.data.entryDate,
        FEndDate: this.data.endDate,
        FItEmpId: this.data.FItEmpId
      }
    }
    personModel.saveEduExp({
      args: {
        data: [param]
      }
    }).then(res => {
      wx.hideLoading()
      wx.navigateBack({
        delta: 1
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '编辑学习经历',
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    this.setData({
      entryDate: year + '-' + month + '-' + day,
      endDate: year + '-' + month + '-' + day,
    })
    if (options.data) {
      var data = JSON.parse(options.data)
      this.setData({
        schoolName: data.FName,
        fullTimeIdx: data.FIsFullTime === '全日制'?0:1,
        eduType: data.FEducationType,
        major: data.FMajor,
        entryDate: data.FStartDate,
        endDate: data.FEndDate,
        FId: data.FId,
      })
    }
    if (options.FItEmpId) {
      this.setData({
        FItEmpId: options.FItEmpId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})